package com.solocredit.finance.ui.user.impl

import android.content.Context
import android.os.Handler
import android.os.Looper
import com.solocredit.finance.mvp.presenter.MvpPresenter
import com.solocredit.finance.ui.user.view.IMullaIUserCouponView
import com.solocredit.finance.utils.DateUtils
import com.solocredit.finance.utils.helper.SharedHelper
import org.json.JSONArray
import org.json.JSONObject

/**
 * MullaFlow
 * @Author wwdeng
 * @CreateTime: 2024/8/12 16:26
 * @Annotation：用户优惠券列表 Presenter 实现
 */
class MullaUserCouponPresenterImpl(context: Context?) : MvpPresenter<IMullaIUserCouponView>(context) {

    /**
     * 获取优惠券列表
     */
    fun loadMullaUserCouponListAction(){
        val dataString = SharedHelper.getMullaSPString(SharedHelper.MULLA_SPKEY.MULLA_USER_COUPON_INFO)
        if (dataString.isNotEmpty()){
            val couponArray = JSONArray(dataString)
            val couponList = arrayListOf<JSONObject>()
            if (couponArray.length() > 0){
                for (i in 0..<couponArray.length()){
                    val item = couponArray.optJSONObject(i)
                    val validitytTime = item.optLong("validitytTime") * 1000
                    item.put("expiresTime", DateUtils.getFormatTime(validitytTime,"dd/MM/yyyy"))
                    item.put("daysLeft", DateUtils.getDaysFromTimestamps(System.currentTimeMillis(),validitytTime))
                    couponList.add(item)
                }
                mvpView?.showMullaUserCouponList(couponList)
            } else {
                mvpView?.showMullaUserCouponEmpty()
            }
        } else {
            mvpView?.showMullaUserCouponEmpty()
        }
        Handler(Looper.getMainLooper()).postDelayed({mvpView?.hideMullaLoading()},500)
    }
}